//
//  MainTabBar.swift
//  Weibo
//
//  Created by 张远文 on 16/4/25.
//  Copyright © 2016年 张远文. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    lazy var composeBtn: UIButton = {
       
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        return btn
        
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = self.bounds.width / 5
        let height = self.bounds.height
        
        var index: CGFloat = 0
        
        
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        
        
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //print(subview)
                
                subview.frame = CGRectOffset(rect, index * width, 0)
                
                index += (index == 1) ? 2 : 1
            }
            
        }
        
        composeBtn.frame = CGRectOffset(rect, 2 * width, -10)
        bringSubviewToFront(composeBtn)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    func setupUI() {
        
        addSubview(composeBtn)
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setupUI()
    }

    
    
}
